import 'package:flutter/material.dart';

Future<int?> sliderDialog(
  BuildContext context, {
  required Widget title,
  required double min,
  required double max,
  required int value,
}) {
  double sliderValue = value.toDouble();
  return showDialog<int>(
    context: context,
    builder: (context) {
      return StatefulBuilder(
        builder: (
          BuildContext context,
          void Function(void Function()) setState,
        ) {
          return AlertDialog(
            title: title,
            content: SizedBox(
              height: 50,
              child: Slider(
                min: min,
                max: max,
                divisions: (max - min).toInt(),
                value: sliderValue,
                onChanged: (value) {
                  setState(() {
                    sliderValue = value;
                  });
                },
              ),
            ),
            actions: [
              Text("当前值: ${sliderValue.toInt()}    "),
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: const Text("取消"),
              ),
              TextButton(
                onPressed: () {
                  Navigator.of(context).pop(sliderValue.toInt());
                },
                child: const Text("确定"),
              ),
            ],
          );
        },
      );
    },
  );
}
